package vno.fux
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	/**
	 * ImgLoader类是一个用于为显示对象加载图片资源的类
	 * 其获取指定url中的图片资源，将资源放入一个Bitmap对象中
	 * 并调用构造函数中所指定的目标(target)的指定方法或者属性来存储此Bitmap对象
	 * 注：当指定的目标为Bitmap对象时，将会把资源直接存储进该对象的bitmapData中
	 * */
	public class ImgLoader extends EventDispatcher
	{
		private var _loader:Loader;
		private var _url:URLRequest;
		private var _target:*;
		private var _container:String;
		
		/**
		 * 创建一个ImgLoader对象
		 * @param url 资源的url地址
		 * @param target 需要获取资源的对象
		 * @param container target对象中用于处理资源的方法或者用于存储资源的属性，
		 * 					如target为Bitmap类型此参数可为空，将会直接将图片存储在target中
		 * @example 下面例子是通过ImgLoader加载资源至一个Sprite对象
		 *	
		 * 	var sp = new Sprite;
		 * 	new ImgLoader("http://www.*.com/image.gif",sp,"addChild");
		 * 
		 * @example 下面例子是通过ImgLoader加载资源至一个Bitmap对象
		 *	
		 * 	var bm = new Bitmap;
		 * 	new ImgLoader("http://www.*.com/image.gif",bm);
		 * */
		public function ImgLoader()
		{
			_loader = new Loader;
		}

		public function load(url:String, target_:*, container:String = null, loadType:String = null)
		{
			try
			{
				if(!(target_ is Bitmap))
				{
					target_[container];
				}
			}
			catch(error:Error)
			{
				throw new ArgumentError("The target "+target_.toString+" don't have the property " +
					"or method \'"+container+"\'");
			}
			_url = new URLRequest(url);
			try
			{
				addListeners()
				_loader.load(_url);
			}
			catch(error:Error)
			{
				trace("Can't load!" + error.message);
			}
			_target = target_;
			_container = container;
		}

		protected function addListeners():void
		{
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
		}
		

		protected function delListeners():void
		{
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
		}
		
		protected function completeHandler(event:Event):void
		{
			if(_target is Bitmap)
			{
				(_target as Bitmap).bitmapData = event.target.content["bitmapData"];
			}
			else if(_target[_container] is Function)
			{
				_target[_container](event.target.content);
			}
			else
			{
				_target[_container] = event.target.content;
			}
			dispatchEvent(event);
			remove();
		}

		protected function progressHandler(event:ProgressEvent):void
		{
			dispatchEvent(event);
		}
		
		protected function ioErrorHandler(event:IOErrorEvent):void
		{
			dispatchEvent(event);
			//trace(evt.type + " at " + this.toString() + _url.url);
			//Alert.show("读取文件出错，未找到文件！");
		}
		
		protected function remove():void
		{
			delListeners();
			_target = null;
			_container = null;
			_loader.unload();
		}

		public function get target():*
		{
			return _target;
		}
	}
}